#ifndef __UART__UART_APP_H__
#define __UART__UART_APP_H__

#include "main.h"
#include "MOTOR/motor_app.h"
#include "MOTOR/pid.h"
#include "WIFI.h"

/**
 ****************************************************************************************************
 * DONE 用户变量区
 ****************************************************************************************************
 */

typedef struct __datapackage
{
    uint8_t ADC_Package1[3][4];
    uint8_t ADC_Package2[3][4];
    uint8_t ADC_Package3[3][4];
    uint8_t ADC_Package4[3][4];
    uint8_t ADC_Package5[3][4];
    uint8_t ADC_Package6[3][4];
    uint8_t ADC_Package7[3][4];
    uint8_t ADC_Package8[3][4];
}datapackage;

//-----------------------------------------------	

#define UART_SEND_MODE 1
#define test 0
#define board 0

#define DATAPACKAGE_SIZE 48

// 定义缓冲区大小
#define RX_BUFFER_SIZE 530
#define TX_BUFFER_SIZE 720


//-----------------------------------------------	

extern uint8_t txBuffer_1[TX_BUFFER_SIZE]; // 发送缓冲区
extern uint8_t rxBuffer_1[RX_BUFFER_SIZE]; // 接收缓冲区

extern UART_HandleTypeDef *bear_huart;
extern UART_HandleTypeDef *G4_huart;
extern UART_HandleTypeDef *bt24_huart;
extern UART_HandleTypeDef *Test_huart;

//接收完成标志
extern uint8_t Receivedsuccee_Flag;

extern uint16_t usart1_packet_len;       
extern uint8_t usart1_ready; // 设置标志位

extern  datapackage txdatapackage;

extern uint8_t GPS_START_FLAG;

extern uint8_t HMI_arry[RX_BUFFER_SIZE];  // 用于存储接收到的数据帧
extern uint8_t HMI_arry_length;       // 当前接收到的数据长度
/**
 ****************************************************************************************************
 * DONE 用户函数区
 ****************************************************************************************************
 */

/**
 * @if Eng 
 * @brief Formatted print to UART interface (similar to printf)
 * @param huart - Pointer to UART handle structure
 * @param fmt - Format string (follows standard printf format)
 * @param ... - Variable arguments matching the format specifiers
 * @result Formatted string is transmitted via UART
 * @else
 * @brief 格式化输出到UART接口(类似printf)
 * @param huart - 指向UART句柄结构的指针 
 * @param fmt - 格式字符串(遵循标准printf格式)
 * @param ... - 匹配格式说明符的可变参数 
 * @result 格式化字符串通过UART传输
 * @endif 
 */
void USART_printf (UART_HandleTypeDef  *huart,char *fmt, ...);

/**
 * @if Eng
 * @brief Sends a data buffer through UART interface
 * @param huart - Pointer to UART handle structure 
 * @param pParket - Pointer to data buffer to send 
 * @param length - Number of bytes to transmit 
 * @result Data buffer is transmitted via UART
 * @else
 * @brief 通过UART接口发送数据缓冲区
 * @param huart - 指向UART句柄结构的指针 
 * @param pParket - 指向要发送的数据缓冲区的指针 
 * @param length - 要传输的字节数 
 * @result 数据缓冲区通过UART传输
 * @endif 
 */
void uart_sendBuffer(UART_HandleTypeDef *huart,uint8_t *pParket,uint16_t length);

/**
 * @if Eng
 * @brief Sends a complete data packet through UART interface
 * @param huart - Pointer to UART handle structure 
 * @param data - Pointer to packet data to send 
 * @param length - Length of packet in bytes
 * @result Complete packet is transmitted via UART 
 * @else 
 * @brief 通过UART接口发送完整数据包
 * @param huart - 指向UART句柄结构的指针 
 * @param data - 指向要发送的数据包的指针
 * @param length - 数据包长度(字节)
 * @result 完整数据包通过UART传输
 * @endif 
 */
void UART_SendPacket(UART_HandleTypeDef *huart,uint8_t *data, uint16_t length);

/**
 * @if Eng
 * @brief Parses received UART packet data 
 * @param huart - Pointer to UART handle structure
 * @param packet - Pointer to received packet data 
 * @param length - Length of received packet in bytes 
 * @result Packet contents are parsed and processed 
 * @else 
 * @brief 解析接收到的UART数据包 
 * @param huart - 指向UART句柄结构的指针
 * @param packet - 指向接收到的数据包的指针 
 * @param length - 接收到的数据包长度(字节)
 * @result 数据包内容被解析和处理
 * @endif 
 */
void UART_ParsePacket(UART_HandleTypeDef *huart,uint8_t *packet, uint16_t length);

/**
 * @if Eng 
 * @brief Converts numerical data to string representation 
 * @param data - Pointer to 3D array containing numerical data 
 * @param buffer - Pointer to output buffer for string results
 * @param ADC_index - Index for ADC data selection
 * @param Num_index - Index for numerical data selection
 * @result Numerical data is converted to string format in buffer 
 * @else 
 * @brief 将数值数据转换为字符串表示
 * @param data - 指向包含数值数据的3D数组的指针
 * @param buffer - 指向字符串结果输出缓冲区的指针
 * @param ADC_index - ADC数据选择索引 
 * @param Num_index - 数值数据选择索引 
 * @result 数值数据被转换为字符串格式存入缓冲区
 * @endif 
 */
void Num_To_String(uint8_t (*data)[6][4],uint8_t (*buffer)[6],uint16_t ADC_index,uint16_t Num_index);

/**
 * @if Eng 
 * @brief Initializes UART communication interface 
 * @param None
 * @result UART interface is configured and ready for communication 
 * @else 
 * @brief 初始化UART通信接口 
 * @param 无 
 * @result UART接口配置完成并准备通信 
 * @endif 
 */
void uart_Init(void);

/**
 * @if Eng 
 * @brief Tests UART communication functionality
 * @param huart - Pointer to UART handle structure to test 
 * @result UART communication is verified through test patterns 
 * @else 
 * @brief 测试UART通信功能 
 * @param huart - 指向要测试的UART句柄结构的指针 
 * @result 通过测试模式验证UART通信 
 * @endif 
 */
void uart_Test(UART_HandleTypeDef *huart);

/**
 * @if Eng 
 * @brief Parses packets received from HMI (Human-Machine Interface) via UART 
 * @param huart - Pointer to UART handle structure
 * @param packet - Pointer to received HMI packet data 
 * @param usart1_rxIndex - Current receive buffer index
 * @result HMI packet is parsed and corresponding actions are taken 
 * @else 
 * @brief 解析通过UART从HMI(人机界面)接收的数据包
 * @param huart - 指向UART句柄结构的指针 
 * @param packet - 指向接收到的HMI数据包的指针
 * @param usart1_rxIndex - 当前接收缓冲区索引
 * @result HMI数据包被解析并执行相应操作
 * @endif 
 */
void UARTHMI_ParsePacket(UART_HandleTypeDef *huart,uint8_t *packet, uint16_t usart1_rxIndex);

#endif
